package com.example.controller;


import com.alibaba.druid.sql.visitor.functions.Now;
import com.example.entity.*;
import com.example.service.*;
import com.example.entity.GoodsVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Slf4j
@CrossOrigin
@Controller
public class HomeController {

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;


    @Autowired
    private OrderService orderService;


    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private SeckillOrderService seckillOrderService;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThymeleafViewResolver resolver;

    @RequestMapping(value = "/index")
    public String Index(Model model, User user) {
        model.addAttribute("name", "zhangsan");

        // DigestUtils.md5DigestAsHex("zhangsan".getBytes());
        System.out.println("--------------" + user);

        return "index";
    }

    @RequestMapping(value = "/detail/{id}")
    public String Detail(Model model, @PathVariable int id) {
        log.info(id + "");
        GoodsVo goodsVo = goodsService.findGoodsVoByID(id);
        log.info(goodsVo + "");
        Date startDate = goodsVo.getStart_date();
        Date endDdate = goodsVo.getEnd_date();
        Date dt = new Date();
        int secStatus = 0;
        int remainSeconds = 0;
        if (dt.before(startDate)) {
            remainSeconds = (int) ((startDate.getTime() - dt.getTime()) / 1000);
        } else if (dt.after(endDdate)) {
            secStatus = 2;//秒杀结束
            remainSeconds = -1;
        } else {
            secStatus = 1;//秒杀开始
            remainSeconds = 0;
        }

        model.addAttribute("remainSeconds", remainSeconds);
        model.addAttribute("secStatus", secStatus);
        model.addAttribute("goodsVo", goodsVo);
        return "detail";
    }

//    @RequestMapping(value = "/doSeckill",method = RequestMethod.POST)
//    @ResponseBody
//    public String doSeckill()
//    {
//       log.info("Hello==========提交过来的数据 ");
//        return "goods";
//    }

    @RequestMapping(value = "/goods")
    public String Goods(Model model) {
        List<GoodsVo> list = goodsService.findGoodsVo();
        System.out.println(list);
        model.addAttribute("list", list);
        return "goods";
    }

    @RequestMapping(value = "/order")
    public String Order(Model model) {
        List<MyOrder> list = orderService.findAll();
        System.out.println(list);

        return "index";
    }


    @RequestMapping(value = "/seckillGoods")
    public String seckillGoods(Model model) {
        List<Seckill_goods> list = seckillGoodsService.findAll();
        System.out.println(list);

        return "index";
    }


    @RequestMapping(value = "/seckillOrder")
    public String SeckillOrder(Model model) {
        List<Seckill_order> list = seckillOrderService.findAll();
        System.out.println(list);

        return "index";
    }


    @RequestMapping(value = "/user")
    public String User(Model model) {
        List<User> list = userService.findAll();
        System.out.println(list);

        return "index";
    }

    //json转对象测试

    @RequestMapping(value = "/test")
    @ResponseBody
    public String Test(Model model) {

        String str = "{\"name\":\"zhangsan\",\"id\":1,\"userName\":\"123\",\"statue\":null,\"tel\":\"133\",\"userPwd\":null,\"validCode\":null,\"pic\":null}";

        try {
            User user = (User) new ObjectMapper().readValue(str, User.class);
            System.out.println("" + user);
        } catch (Exception e) {
            System.out.println("" + e);
        }

        return "index";
    }


    @RequestMapping(value = "/pagetest")
    @ResponseBody
    public String PageTest(Model model, HttpServletRequest request, HttpServletResponse response) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String) valueOperations.get("pagetest");
        if (!StringUtil.isNullOrEmpty(html)) {
            return html;
        }

        model.addAttribute("name", "zhangsan");
        model.addAttribute("age", 12);
        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());

        html = resolver.getTemplateEngine().process("pagetest", webContext);
        if (!StringUtil.isNullOrEmpty(html)) {
            valueOperations.set("pagetest", html, 60, TimeUnit.MINUTES);
        }
        return html;
    }


    @RequestMapping(value = "test1")
    @ResponseBody
    public Map<String, Object> Test1(Model model, User user) {
        log.info(user + "");
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("name", "123");
        map.put("age", 456);
        map.put("student", user);

        return map;
    }

}
